
Yacon.widget.grid.list.ListGrid = function (conText)
{
	this.absolutePage      	= 0;
	this.pageSize           = 0;
	
	Yacon.apply(this,conText);
	this.create();	

};

Yacon.widget.grid.list.ListGrid.Extend(Yacon.widget.grid.list.BaseGrid,{
	create:function()
	{
		this.baseCreate();	
		this.analyseMaster();
		this.analyseDetailPage();
			
	},	
	convertDataset:function()
	{
		
		if (this.headControl)
			this.headControl.convertDataset();
		
		if (this.viewControl)
			this.viewControl.convertDataset();
		
	},
	setPageSize:function(pageSize)
	{
		this.pageSize = pageSize;
	},
	getPageSize:function()
	{
		if (this.pageSize <= 0)
			this.pageSize = Yacon.getInt(this.element.getAttribute("pageSize"));
		
		return this.pageSize;
	},
	
	getRecordCount:function()
	{
		return this.viewControl.getRecordCount();
	},
	getPageCount:function()
	{
		return this.viewControl.getPageCount();	
	},	
	setAbsolutePage:function(absolutePage)
	{
		this.absolutePage     = absolutePage;
	},	
	getAbsolutePage:function()
	{
		if (this.absolutePage <= 0)
			this.absolutePage = Yacon.getInt(this.element.getAttribute("absolutePage"));
		
		return this.absolutePage;			
	},
	
	setExcuteMethod:function(excuteMethod)
	{
		this.getDataSet();
		this.excuteMethod = excuteMethod;		
		this.dataSet.setExcuteMethod(excuteMethod);
	},
	
	getExcuteMethod:function()
	{
		if (!this.excuteMethod)
			this.excuteMethod = this.element.getAttribute("refreshMethod");
			
		return this.excuteMethod;
	},
	
	setRefreshMethod:function(refreshMethod)
	{
		this.getDataSet();
		this.refreshMethod = refreshMethod;		
		this.dataSet.setRefreshMethod(refreshMethod);
	},
	
	getRefreshMethod:function()
	{
		if (!this.refreshMethod)
			this.refreshMethod = this.element.getAttribute("refreshMethod");
			
		return this.refreshMethod;
	},	
	clearRecord: function()
	{
		while(this.gridView.rows.length >0)
		{
			this.gridView.removeChild(rows[0]);
		}
	},
	setForeignField:function(fieldIndex,fieldValue)
	{
		this.getDataSet();
		this.dataSet.setForeignField(fieldIndex,fieldValue);
	},
	setWhereField:function(fieldIndex,fieldValue)
	{
		this.getDataSet();
		this.dataSet.setWhereField(fieldIndex,fieldValue);
	},
	addAttribute:function(fieldIndex,fieldValue)
	{
		this.getDataSet();
		this.dataSet.addAttribute(fieldIndex,fieldValue);
	},
	getFieldValue:function(fieldIndex)
	{
		this.getDataSet();
		return this.dataSet.getFieldValue(fieldIndex);
	},
	getFieldText:function(fieldIndex)
	{
		this.getDataSet();
		return this.dataSet.getFieldText(fieldIndex);
	},
	
	post:function(type)
	{
		this.getDataSet();
		this.dataSet.post(type);
	},
	/**
	 *  刷新状态
	 */
	refresh:function(pageIndex)
	{
	
/*		alert("pageIndex="+pageIndex);*/
		if(Yacon.getInt(pageIndex) > Yacon.getInt(this.getPageCount()))
			return;
			
		if (Yacon.getInt(pageIndex)>0)
			this.dataSet.addAttribute("isclear","false");
		else
			this.dataSet.addAttribute("isclear","true");
		
		if (Yacon.getInt(pageIndex) <= 0 )
			pageIndex = 1;
		this.setAbsolutePage(pageIndex);
			
		this.dataSet.addAttribute("absolutePage",pageIndex);
		this.dataSet.addAttribute("pageSize",this.getPageSize());
		this.dataSet.setRefreshMethod(this.refreshMethod);
		this.dataSet.refresh();
	},

	afterRefresh:function()
	{
		if (this.dataSet.getResultType().isEqual("2"))
		{
			this.afterHtml();
		}else
		{
			this.afterDataSet();
		}
		
	},
	afterHtml:function()
	{
	
		if (!this.viewControl)
			return;	
	
		this.viewControl.setElementTemplate(this.dataSet.getDataValue());
				
		this.broadCastDetail();
		
		if (this.bottomControl)
			this.bottomControl.evalAttrValue();
		
		this.setWidth();
	},
	afterDataSet:function()
	{
		
		if (!this.viewControl)
			return;	
			
		this.viewControl.creatElementTemplate();
			
		var  recordCount  = Yacon.getInt(this.dataSet.getAttribute("recordCount"))
		var  pageCount  = Yacon.getInt(this.dataSet.getAttribute("pageCount"))
		
		this.viewControl.setRecordCount(recordCount);
		this.viewControl.setPageCount(pageCount);
		this.viewControl.validateCheck();
		
		this.broadCastDetail();
		
		if (this.bottomControl)
			this.bottomControl.evalAttrValue();
		
		this.dataSet.addAttribute("absolutePage",this.getAbsolutePage());
		this.dataSet.addAttribute("pageSize",this.getPageSize());
	},
	
	analyseMaster:function()
	{
		if (this.element && this.element.getAttribute("masterTable"))
		{
			this.masterTable = new Yacon.util.Container();
			this.masterTable.setJsonStr(this.element.getAttribute("masterTable"));
			
			if (window[this.masterTable.getAttrValue("masterID")])
			{
				this.materGrid = window[this.masterTable.getAttrValue("masterID")];
				
				window[this.masterTable.getAttrValue("masterID")].addDetailTable(this);
				if (window[this.masterTable.getAttrValue("masterID")].dataSet.getRecordCount()>0)
					this.masterfreshDetail(window[this.masterTable.getAttrValue("masterID")].dataSet);
				//从表数据处理 - 待添加
				
			}	
		}
	},
	broadCastDetail:function ()
	{
		
		if (this.detailTables && this.dataSet.getRecordCount()>0)
		{
			
			for(var i=0;i<this.detailTables.length;i++)
			{
				this.detailTables[i].masterfreshDetail(this.dataSet);				
			}
		}		
	},
	masterfreshDetail:function (masterDataSet)
	{
		if (this.masterTable)
		{
			this.masterTable.beforeFirst();
			while(this.masterTable.next())
			{
				var filename    = this.masterTable.getFieldValue("fieldName");
				var foreignName = this.masterTable.getFieldValue("foreignName");
				this.setForeignField(filename,masterDataSet.getFieldValue(foreignName));
			}
			this.refresh();
		}
	},
	analyseDetailPage:function()
	{
		if (this.element.getAttribute("detailPage"))
		{
			this.detailPage = new Yacon.util.Container();
			this.detailPage.setJsonStr(this.element.getAttribute("detailPage"));
		}
	},	
	addDetailTable:function(detailTable)
	{
		if (!this.detailTables)
			this.detailTables = new Array();
		
		this.detailTables.add(detailTable);
	}
	
});

